﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using System.Text.RegularExpressions;

namespace University.Workshop.Business
{
    public static class UpdateUserAccountManager
    {
        public static void ChangeUserPassword(
            string email,
            string currentPassword,
            string newPassword,
            string retypedPassword
            )
        {
            if (string.IsNullOrEmpty(email) ||
                string.IsNullOrEmpty(currentPassword) ||
                string.IsNullOrEmpty(newPassword) ||
                string.IsNullOrEmpty(retypedPassword)
                )
            {
                throw new ValidationException("Required fields CAN'T be empty");
            }
            User existsUser = UserDataAccess.GetUserByEmail(email);
            if (existsUser == null)
            {
                throw new BusinessException("User doesn't exist");
            }
            string passwordPattern = "\\w{6,20}";
            string passwordPattern1 = "\\d+";
            if (!(Regex.IsMatch(newPassword, passwordPattern) &&
                  Regex.IsMatch(newPassword, passwordPattern1)
               ))
            {
                throw new ValidationException("Invalid New Password");
            }
            if (newPassword != retypedPassword)
            {
                throw new ValidationException("New Passwords don't match");
            }

            UserDataAccess.CancelUserAccount(existsUser, AccountState.Inactive);
        }

        public static void CancelUserAccount(string email, string password)
        {

            if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
            {
                throw new ValidationException("Required fields CAN'T be empty");
            }

            User existsUser = UserDataAccess.GetUserByEmail(email);
            if (existsUser == null)
            {
                throw new BusinessException("User doesn't exist");
            }

            UserDataAccess.CancelUserAccount(existsUser, AccountState.Inactive);

        }


    }
}
